package com.qiche.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.qiche.aspect.Result;
import com.qiche.service.RegionWelfareService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.mapper.UserRoleMapper;
import com.qiche.model.RegionWelfare;

import tk.mybatis.mapper.entity.Example;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("welfareService")
public class RegionWelfareServiceImpl extends BaseService<RegionWelfare> implements RegionWelfareService {
	@Resource
	private UserRoleMapper userRoleMapper;

	@Override
	public PageInfo<RegionWelfare> selectByPage(RegionWelfare policy, int page, int limit) {
		Example example = new Example(RegionWelfare.class);
		Example.Criteria criteria = example.createCriteria();
		if (policy.getRegionId() != null && policy.getRegionId() != 0) {
			criteria.andEqualTo("regionId", policy.getRegionId());
		}
		// 分页查询
		PageHelper.startPage(page, limit);
		List<RegionWelfare> policyList = selectByExample(example);
		return new PageInfo<>(policyList);
	}

	@Override
	public RegionWelfare selectById(Long welfareId) {
		Example example = new Example(RegionWelfare.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", welfareId);
		List<RegionWelfare> userList = selectByExample(example);
		if (userList.size() > 0) {
			return userList.get(0);
		}
		return null;
	}

	@Override
	public List<RegionWelfare> selectByRegionId(Long regionId) {
		Example example = new Example(RegionWelfare.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("regionId", regionId);
		List<RegionWelfare> userList = selectByExample(example);

		return userList;

	}
	

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = { Exception.class })
	public Result delRegionWelfare(Long welfareId) {
		int deleteCount=mapper.deleteByPrimaryKey(welfareId);
		if(deleteCount>0){
			return Result.createBySuccessMessage("删除成功");
		}
		return Result.createByErrorMessage("删除失败");

	}

}
